var express = require('express');
var router = express.Router();
const billModel = require('../../config/model')
/* GET home page. */
router.get('/', function(req, res, next) {
  billModel.find().then((data) => { 
    res.render('index', { title: 'Express', data:data.reverse() });
  }).catch(err=>{
    res.status(500).send(err)
  })
  
});
router.get('/create',function(req,res,next){
  res.render('create')
})
router.get('/success',function(req,res,next){
  res.render('success')
})

router.post('/add',function(req,res,next){
  billModel.create(req.body).then(()=>{
    res.render('success',{type:'success',title:'添加成功',url:'/'})
  }).catch((error) => { 
    res.render('success',{type:'error',title:'添加失败',url:'/create'})
  })
})

router.get('/delete/:id',function(req,res,next){
  billModel.deleteOne({_id:req.params.id}).then(()=>{
    res.render('success',{type:'success',title:'删除成功',url:'/'})
  }).catch(() => { 
    res.render('success',{type:'error',title:'删除失败',url:'/'})

   })
})






// router.all('*',function(req,res,next){
//   res.render('error',{message:'404 not found',error:{status:404,stack:'not found'}})
// })



module.exports = router;
